<?php

namespace App\Http\Controllers\PC;

use App;
use App\Common\Status;
use App\Helper\GuzzleHttp;
use App\Helper\Helper;
use App\Http\Logic\PC\JobLogic;
use App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class JobController extends BaseController
{
    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getJobs(Request $request)
    {
        $params = $request->query();

        $pageSize = $request->query("pageSize", Status::PAGE_SIZE);
        $page     = $request->query("page", Status::PAGE);
        $r        = JobLogic::getJobs($params, $pageSize, $page);

        return $this->success(Status::SUCCESS, 'success', $r);
    }

    /**
     * 职位详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function getJobsDt($id)
    {
        $r = JobLogic::getJobsDt($id);

        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * 招聘id [表：recruit_job]
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDeliverJobs($id)
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }
        JobLogic::setDeliverJobs($id);

        return $this->success(Status::SUCCESS, 'success', []);
    }


    /**
     * 获取用户投递记录
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRecruitJobNotes($type)
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }
        $r = JobLogic::getRecruitJobNotes($type);

        return $this->success(Status::SUCCESS, 'success', $r);
    }

    /**
     * 获取用户投递记录
     * @param $recruit_id = recruit_job_notes_auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRecruitJobNotesDtl($recruit_id)
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }
        $r = JobLogic::getRecruitJobNotesDtl($recruit_id);

        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAllCity()
    {
        $data = DB::table('recruit_city')->select(["id", "name", "parentid", "leveltype"])->get();
        $tree = [];
        if ($data->isNotEmpty()) {
            $tree = App\Http\Logic\PC\CityLogic::getcityTree($data->map(function ($value) {
                return (array)$value;
            })
                ->toArray());
        }
        return $this->success(Status::SUCCESS, "success", $tree);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function location(Request $request)
    {
        $lat = $request->query("lat");
        $lon = $request->query("lon");
        if (empty($lat) || empty($lon)) {
            return $this->error(Status::ERROR, "经纬度");
        }
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $lat . ',' . $lon . '&key=' . Status::TXKEY;
        $d = GuzzleHttp::GuzzleGet($url, 2, []);
        if($d['code'] == Status::SUCCESS) {
            $info = json_decode($d['contents'], true);
            return $this->success(Status::SUCCESS, "success", $info);
        }else {
            return $this->error(Status::ERROR, "解析失败");
        }
    }
}
